$(function (){

    var appPath = $("#appPath").val()

    //点击房型..根据房型..获得空房间号
    $("#redact_div_select").click(function (){
        //获取房型的号码
        var houseTypeId = $("#redact_div_select").val()

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=roomRates&houseTypeId="+houseTypeId,
            "dataType" : "json",
            "success" : function (result){
                var html = "";
                for (let i = 0; i < result.length; i++) {
                    var roomNumber = result[i].roomNumber
                    html += "<option value='"+roomNumber+"'>"+roomNumber+"</option>"
                }
                $("#redact_div_select2").html(html)
            },
            "error" : function (){
                alert("错")
            }
        })
    })

    //点击房型..根据房型..获得房型的价格
    $("#redact_div_select").click(function (){
        //获取房型号码
        var typeId = $("#redact_div_select").val()
        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=roomPrice&houseTypeId="+typeId,
            "dataType" : "json",
            "success" : function (result){

                if (typeId <= 0){
                    $("#redact_price").val("0.00")
                }else {
                    $("#redact_div_select").removeClass()
                    $("#redact_price").val(result.price)
                }
            },
            "error" : function (){
                alert("错")
            }
        })
    }).blur(function (){
        //获取手机号码
        var phone = $("#redact_mobilePhone").val()
        //获取金额
        var price = $("#redact_price").val()

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=vip&mobilePhone="+phone,
            "dataType" : "json",
            "success" : function (result){
                //获取折扣
                var discount = result.discount
                //判断是否是会员
                if (!isNaN(result)){
                    price = price * discount
                    let money = parseFloat(price).toFixed(2)
                    $("#redact_price").val(money)
                }else {
                    price = price
                    let money = parseFloat(price).toFixed(2)
                    $("#redact_price").val(money)
                }
            },
            "error" : function (){
                alert("错")
            }
        })
    })


    //退房时间失焦计算开放几天，开放天数*房间价格
    $("#redact_checkOutDate").blur(function (){
        //获取房型号码
        var typeId = $("#redact_div_select").val()

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=roomPrice&houseTypeId="+typeId,
            "dataType" : "json",
            "success" : function (result){
                if (typeId <= 0){
                    $("#redact_price").val("0.00")
                }else {
                    //开房时间
                    let inDat = new Date();
                    //退房时间
                    let outDate = $("#redact_checkOutDate").val()
                    //计算毫秒
                    let second = parseInt(new Date(outDate).getTime() / 1000) - parseInt(new Date(inDat).getTime() / 1000)
                    //计算开房天数
                    let day = parseInt(second / 60 / 60 / 24);
                    //计算小时
                    let hour = parseInt(second / 60 / 60)
                    //是否满足一天
                if (hour % 24 == 0){
                    day = day;
                }else if (hour % 24 != 0){
                    day = day +1
                }
                    //计算价格*开房天数
                    let price = result.price * day;
                    $("#redact_price").val(price)
                }
            },
            "error" : function (){
                alert("错")
            }
        })
    })


    //作用域原因..此房间号是刚进房间的房间号
    var redact_div_select2 = $("#redact_div_select2").val()
    //修改房间状态归0
    $(".redact_form_button").click(function (){

        $.ajax({
            "url" : appPath+"/formVerify",
            "type" : "post",
            "data" : "type=setState&roomNumber="+redact_div_select2,
            "dataType" : "json",
            "success" : function (result){

                if (result <= 0){
                    alert("失败!")
                }
            },
            "error" : function (){
                alert("错")
            }
        })
    })

    // //电话号码失焦，验证是否是会员
    // $("#redact_mobilePhone").blur(function (){
    //     var phone = $("#redact_mobilePhone").val()
    //     var price = $("#redact_price").val()
    //     $.ajax({
    //         "url" : appPath+"/formVerify",
    //         "type" : "post",
    //         "data" : "type=vip&mobilePhone="+phone,
    //         "dataType" : "json",
    //         "success" : function (result){
    //             var discount = result.discount
    //             if (result != null){
    //                 price = price * discount
    //                 $("#redact_price").val(price)
    //             }else {
    //                 price = price
    //                 $("#redact_price").val(price)
    //             }
    //         },
    //         "error" : function (){
    //             alert("错")
    //         }
    //     })
    // })
})
